class Matrix{
    constructor(arr){
        this.data=arr;
        this.shape=[arr.length,arr[0].length];
    }
    add(mat){
        var res=[];
        for(var i in this.data){
            var linearr=[];
            for(var j in this.data[i]){
                linearr.push(this.data[i][j]+mat.data[i][j]);
            }
            res.push(linearr);
        }
        return new Matrix(res);
    }
    sub(mat){
        var res=[];
        for(var i in this.data){
            var linearr=[];
            for(var j in this.data[i]){
                linearr.push(this.data[i][j]-mat.data[i][j]);
            }
            res.push(linearr);
        }
        return new Matrix(res);
    }
    mul(v){
        if(typeof v=='number'){
            var res=[];
            for(var i in this.data){
                var linearr=[];
                for(var j in this.data[i]){
                    linearr.push(this.data[i][j]*v);
                }
                res.push(linearr);
            }
            return new Matrix(res);
        }
    }
    getRow(id){
        return new Vector(this.data[id]);
    }
    getCol(id){
        var res=[];
        for(var i in this.data){
            res.push(this.data[i][id]);
        }
        return new Vector(res);
    }
    print(){
        var str='[';
        for(var i=0;i<this.shape[0];i++){
            for(var j=0;j<this.shape[1];j++){
                str+=this.data[i][j]+' ';
            }
            str+='\n';
        }
        str+=']';
        console.log(str);
    }
    sum(){
        var res=0;
        for(var i in this.data){
            res+=this.getRow(i).sum();
        }
        return res;
    }
    mean(){
        var res=this.sum();
        return res/(this.shape[0]*this.shape[1]);
    }
}

exports.Matrix=Matrix;